/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package PlayerShell;
import JAppToolKit.JRunHelper;
import PlayerComponent.JPlayerResponse;
import java.io.*;

/**
 *
 * @author wcss
 */
public class JMplayerLoader 
{
    
    public static String MPLAYER="/usr/bin/mplayer";
    public static JPlayerResponse playerResponse = null;
    public static final String defaultBgColorStr = "0x010101";
    public static String defaultSoundDevice = "alsa";
    public static String defaultVideoDevice = "x11";
    
    /**
     * 生成mpayer指令
     * @param vo 视频模式 (linux下只能用x11和xv还有一个神码的,windows下用directX)
     * @param ao 音频模式 (linux下为 oss 或 alsa)
     * @param wId awt窗口句柄
     * @param colorKey 视频窗口的背景色
     * @param videofile 播放文件路径
     * @return 
     */
    public static String makeMplayerCmdLine(String vo,String ao,String wId,String colorKey,String videofile)
    {
        //调用mplayer命令行 
        return MPLAYER + " -vo " + vo + " -ao " + ao + " -identify -slave -wid " + wId + " -colorkey " + colorKey + " -osdlevel " + String.valueOf(1) + " -quiet " + videofile;
    }
    
        /**
     * 生成mpayer指令
     * @param vo 视频模式 (linux下只能用x11和xv还有一个神码的,windows下用directX)
     * @param ao 音频模式 (linux下为 oss 或 alsa)
     * @param wId awt窗口句柄
     * @param colorKey 视频窗口的背景色
     * @param videofile 播放文件路径
     * @param size 屏幕大小
     * @return 
     */
    public static String makeMplayerCmdLine(String vo,String ao,String wId,String colorKey,String videofile,int videowidth)
    {
        //调用mplayer命令行 
        return MPLAYER + " -af volume=-10 -softvol -softvol-max 200 -vo " + vo + " -ao " + ao + " -identify -slave -wid " + wId + " -colorkey " + colorKey + " -osdlevel " + String.valueOf(1) + " -quiet " + videofile + " -xy " + videowidth;
    }

    /**
     * 
     * @param pFile 文件路径
     * @param hWnd  窗口句柄
     * @return
     * @throws IOException 
     */
    public static Process openPlayer(final File pFile, final long hWnd,final int videowidth) throws IOException, Exception
    {
        String cmd = null;        
        if (videowidth <= 0)
        {
            cmd = makeMplayerCmdLine(JMplayerLoader.defaultVideoDevice, JMplayerLoader.defaultSoundDevice, String.valueOf(hWnd), JMplayerLoader.defaultBgColorStr, pFile.getAbsolutePath());
        }else
        {
            cmd = makeMplayerCmdLine(JMplayerLoader.defaultVideoDevice, JMplayerLoader.defaultSoundDevice, String.valueOf(hWnd), JMplayerLoader.defaultBgColorStr, pFile.getAbsolutePath(),videowidth);
        }
        System.out.println(cmd);        
        final Process lProcess = JRunHelper.runSysCmd(cmd,false);
        final InputStream stderr = lProcess.getErrorStream();
        final InputStream stdin = lProcess.getInputStream();
        new Thread(new Runnable() 
        {
            public void run() 
            {
                try 
                {
                    final BufferedReader lReader = new BufferedReader(new InputStreamReader(stderr, "UTF-8"));
                    for (String l = lReader.readLine(); l != null; l = lReader.readLine()) {
                        if (playerResponse != null)
                        {
                            playerResponse.responseContent(l,true);
                        }
                    }
                } catch (Throwable t) {
                    t.printStackTrace();
                }

            }
        }).start();
        new Thread(new Runnable() {
            public void run() {
                try {
                    final BufferedReader lReader = new BufferedReader(new InputStreamReader(stdin, "UTF-8"));
                    for (String l = lReader.readLine(); l != null; l = lReader.readLine()) {
                        if (playerResponse != null)
                        {
                            playerResponse.responseContent(l,false);
                        }
                    }
                } catch (Throwable t) {
                    t.printStackTrace();
                }
            }
        }).start();
        return lProcess;
    }

}

